﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using Common;
using Common.Tools;
using MyGameServer.Manager;
using Common.Model;

namespace MyGameServer.Handler
{

    class ThrowItemHandler : BaseHandler
    {
        public ThrowItemHandler()
        {
            OpCode = OperationCode.ThrowItem;
        }
        public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
        {
            Dictionary<string, object> ret_Params = (Dictionary<string, object>)DictTool.GetValue<byte, object>(operationRequest.Parameters, (byte)ParameterCode.ThrowItem);
            int playerId_from = (int)ret_Params["playerId_from"];
            int playerId_to = (int)ret_Params["playerId_to"];
            int throwItem_id = (int)ret_Params["throwItem_id"];

            OperationResponse response = new OperationResponse(operationRequest.OperationCode);
            Dictionary<byte, object> data = new Dictionary<byte, object>();

            Dictionary<string, object> Params = ret_Params;
            data.Add((byte)ParameterCode.ThrowItem, Params);
            response.Parameters = data;
            //response.ReturnCode = (short)Common.ReturnCode.Success;

            peer.SendOperationResponse(response, sendParameters);

            
            //广播
            Room room = Functions.GetRoomFromRoomListByPlayerId(MyGameServer.Instance.roomList, peer.uuid);
            foreach (var ntfPeer in MyGameServer.Instance.peerList)
            {
                if (string.IsNullOrEmpty(ntfPeer.username) == false)
                {
                    bool isInRoom = Functions.IsPlayerInRoom(room, ntfPeer.uuid);
                    if (isInRoom)
                    {
                        EventData ed = new EventData((byte)EventCode.ThrowItem);
                        Dictionary<byte, object> dataEvent = data;
                        
                        ed.Parameters = dataEvent;
                        ntfPeer.SendEvent(ed, sendParameters);
                    }
                }
            }
        }
    }
}